﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using ExtAspNet;
using Sale_Operation;
using DevExpress.XtraCharts;
using Sale_Common;
namespace SaleSystem
{
    public class PageBase : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!IsPostBack)
            {
                if (PageManager.Instance != null)
                {
                    HttpCookie themeCookie = Request.Cookies["Theme"];
                    if (themeCookie != null)
                    {
                        string themeValue = themeCookie.Value;
                        PageManager.Instance.Theme = (Theme)Enum.Parse(typeof(Theme), themeValue, true);
                    }

                    HttpCookie langCookie = Request.Cookies["Language"];
                    if (langCookie != null)
                    {
                        string langValue = langCookie.Value;
                        PageManager.Instance.Language = (Language)Enum.Parse(typeof(Language), langValue, true);
                    }
                }
            }

        }
        protected void FormatGrid(Grid gv)
        {
            gv.PageSize = LinkService.PageSize;
            gv.RowHeight = "25";
            gv.AutoScroll = true;
            
        }
        /// <summary>
        /// 生成柱状图
        /// </summary>
        /// <param name="Chart">控件对象</param>
        /// <param name="DataName">显示的数据名称</param>
        /// <param name="DataValue">显示的数据值</param>
        /// <param name="Dt">数据源</param>
        /// <param name="HeaderText">标题文本</param>
        /// <param name="SmallTitle">统计文本</param>
        public  void GetChart(DevExpress.XtraCharts.Web.WebChartControl Chart, string DataName, string DataValue, DataTable Dt, string HeaderText, string SmallTitle)
        {
            try
            {
                Chart.Height = Unit.Pixel(460);
                Chart.Width = Unit.Pixel(800);
                Chart.Series.Clear();
                Chart.Titles.Clear();
                Chart.BorderOptions.Visible = false;
                Chart.Legend.Border.Visible = false;
                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    Series series = new Series(Dt.Rows[i][DataName].ToString(), ViewType.Bar3D);
                    series.Label.Visible = true;
                    series.Points.Add(new SeriesPoint(SmallTitle, new double[] { double.Parse( ValueHandler.GetDecNumberValue (Dt.Rows[i][DataValue].ToString()).ToString()) }));
                    Chart.Series.Add(series);
                }
                Chart.Titles.Clear();
                ChartTitle ct = new ChartTitle();
                ct.Text = HeaderText;
                Chart.Titles.Add(ct);
            }
            catch (Exception ex)
            { 
            }
        }
        /// <summary>
        /// 生成饼状图
        /// </summary>
        /// <param name="Chart">控件对象</param>
        /// <param name="dt">数据源</param>
        /// <param name="DataName">名称</param>
        /// <param name="DataValue">值</param>
        public void GetPie(DevExpress.XtraCharts.Web.WebChartControl Chart, DataTable dt, string DataName, string DataValue, string HeaderText)
        {
            try
            {
                Chart.Height = Unit.Pixel(460);
                Chart.Width = Unit.Pixel(800);
                Chart.Series[0].Points.Clear();
                DevExpress.XtraCharts.SeriesPoint p = null;
                //double[] d = { 0 };

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        p = new DevExpress.XtraCharts.SeriesPoint();
                        double[] d = { double.Parse( ValueHandler.GetDecNumberValue( dt.Rows[i][DataValue]).ToString()) };
                        p.Argument = dt.Rows[i][DataName].ToString();
                        p.ArgumentSerializable = dt.Rows[i][DataName].ToString();
                        p.Values = d;
                        Chart.Series[0].Points.Add(p);
                    }
                }
                Chart.Titles.Clear();
                
                ChartTitle ct = new ChartTitle();
                ct.Text = HeaderText;
                Chart.Titles.Add(ct);
            }
            catch
            {

            }
        }
        #region Property
        private string _strCondition;
        /// <summary>
        /// 子类可用
        /// </summary>
        public string strCondition
        {
            get
            {
                return _strCondition;
            }
            set
            {
                _strCondition=value;
            }
        }
        /// <summary>
        /// 子类重写
        /// </summary>
        public virtual void InitCondition()
        { 
        }
        public int iRecordCount;
        public DataTable dtList;
        public object SModel;
        /// <summary>
        /// 如果是编辑界面，记录编辑界面的ID
        /// </summary>
        public int Id
        {
            get
            {
                if (ViewState["Id"] != null)
                    return int.Parse(ViewState["Id"].ToString());
                else
                    return 0;
            }
            set
            {
                ViewState["Id"] = value;
            }
        }
        #endregion
    }
}
